package com.dy.spring.message;

import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author huangdeyao
 * @date 2019/7/2 11:16
 */
@Log4j2
@Component
public class MqReceiver {

    /**
     * 手动创建队列（需要在mq页面创建myQueue队列）
     *
     * @param message
     */
    @RabbitListener(queues = "myQueue")
    public void process(String message) {
        log.info("MqReceiver: {} ", message);
    }

    /**
     * 自动创建队列
     *
     * @param message
     */
    @RabbitListener(queuesToDeclare = @Queue("autoQueue"))
    public void autoCreateMq(String message) {
        log.info("接受自动创建的队列消息: {} ", message);
    }

    /**
     * 自动绑定exchange
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("autoExchangeQueue"),
            exchange = @Exchange("autoExchange")
    ))
    private void autoExchange(String message) {
        log.info("自动绑定exchange: {} ", message);
    }

    /**
     * 给老王发通知，绑定autoExchange
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("autoExchange"),
            key = "lw",
            value = @Queue("lwQueue")
    ))
    private void noticeMrW(String message) {
        log.info("老王, {} ", message);
    }

    /**
     * 给老张发通知，绑定autoExchange
     *
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("autoExchange"),
            key = "lz",
            value = @Queue("lzQueue")
    ))
    private void noticeMrZ(String message) {
        log.info("老张, {} ", message);
    }
}
